//游戏平台信息
"use strict"
const mvc = require('cosjs.mvc');
const dbase = require('../dbase');
const cosjs   = require('cosjs');

class branch extends dbase.mongodb {
    constructor(){
        super('branch');
    }

    add(info){
        return this.insert(info).then(()=>{
            return creat_branch_indexes(info["_id"]);
        })
    }
}

module.exports = function(){
    return new branch();
}



function creat_branch_indexes(branch){
    let indexes = [
        ['openid',{"guid":1},{}],
        ['openid',{"openid":1},{}],
        ['openid',{"access":1},{}],
    ]

    return cosjs.pool.connect('mongodb').then(client =>{
        let dbname = ["gm",branch].join("-");
        let mongodb = client.db(dbname);
        let multi = mvc.library('multi',indexes,function (arr) {
            let coll = mongodb.collection(arr[0]);
            return coll.createIndex(arr[1],arr[2]);
        });
        multi.breakOnError = true;
        return multi.start();
    });
}